<template>
  <div>
    <el-dialog
      title="新增部门"
      :visible="showDialog"
      @close="close"
    >
      <el-form ref="form" label-width="150px" :model="formData" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="formData.code" placeholder="请输入6到10位编码" />
        </el-form-item>
        <el-form-item label="部门负责人">

          <el-select v-model="formData.managerId" style="width:100%" placeholder="请输入部门负责人">
            <el-option
              v-for="item in managerList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>

        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="formData.introduce" placeholder="请输入介绍信息" />
        </el-form-item>
      </el-form>
      <el-row type="flex" style="padding-left:270px">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { departmentList, userSimple, addDept, getDetail } from '@/api/department'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    depId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      formData: {
        code: '',
        name: '',
        managerName: '',
        introduce: '',
        pid: null,
        managerId: ''

      },
      rules: {
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
          // 调用异步接口并获取数据
            const list = await departmentList()
            list.some(item => {
              if (item.code === value) {
                callback('编码已存在')
              } else {
                callback()
              }
            })
            // 检查返回的数据
            console.log(list)
          }, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
          // 调用异步接口并获取数据
            const list = await departmentList()
            list.some(item => {
              if (item.name === value) {
                callback('部门已存在')
              } else {
                callback()
              }
            })
            // 检查返回的数据
            console.log(list)
          }, trigger: 'blur' }

        ],
        managerName: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
          // 调用异步接口并获取数据
            const list = await departmentList()
            list.some(item => {
              if (item.managerName === value) {
                callback('部门负责人已存在')
              } else {
                callback()
              }
            })
            // 检查返回的数据
            console.log(list)
          }, trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入介绍', trigger: 'blur' },
          { validator: async(rule, value, callback) => {
          // 调用异步接口并获取数据
            const list = await departmentList()
            list.some(item => {
              if (item.introduce === value) {
                callback('部门介绍已存在')
              } else {
                callback()
              }
            })
            // 检查返回的数据
            console.log(list)
          }, trigger: 'blur' }
        ]
      },
      managerList: [
        {
          id: 1,
          lable: '黄金糕',
          value: '选项1'
        }, {
          id: 2,
          lable: '觉得哈克',
          value: '选项二'
        }
      ] // 负责人列表
    }
  },
  async created() {
    this.handle()
  },
  methods: {
    async handle() {
      const res = await userSimple()
      console.log(res, 143)
      this.managerList = res
    },
    close() {
      this.$refs.form.resetFields()
      console.log(151, '触发了')
      this.$emit('update:showDialog', false)
    },
    async  submit() {
      await this.$refs.form.validate()
      this.formData.pid = this.depId
      await addDept(this.formData)
      this.$message.success('新增成功')
      this.close()
      this.$emit('updateEvent')
    },
    async getDtailList() {
      const res = await getDetail(this.depId)
      this.formData = res
      console.log(res, 169)
    }
  }
}
</script>

<style scoped>
</style>
